var com = com || {};
com.em = com.em || {};

com.em.Privilege = function () {
	// for show message to client
	this.alert = null;
	// for data table
	this.table = null;
	// init plugin jgrowl
	this.initAlert();
	// for register validator
	this.validator;
	// urls
	this.url = {};
};
com.em.Privilege.prototype = {
	initAlert : function() { with (this) {
		alert = new com.em.Alert();
	}},
	
	setValidatorForm : function(selector) {
		this.validator = $(selector).validate({
			rules: {
				roleFilter: {
					required: true
				}
			}
		});
	},
	
	/**
	 * Configure table and elements
	 *  
	 * @param selector
	 */
	configureTable:function(selector) { with (this) {
		table = $(selector).dataTable({
			"bProcessing"   : true,
			"bFilter"       : false,
			"bSort"         : false,
			"bInfo"         : false,
			"bLengthChange" : false,
			"bServerSide"   : true,
			"sAjaxSource"   : url.toTable,
			"sPaginationType": "full_numbers",
			"aoColumns"     : [{
					"sWidth": "20px",
					"sClass": "checkColumn",
					fnRender : function (oObj){
						return '<input type="checkbox" name="privileges[]" value="'+oObj.aData[0]+'">';
			   		}
				},null,null,null
			],
			"fnDrawCallback" : function() {
				
			}
		});
	}},
	
	/**
	 * Configure drop down module, controller, action and role 
	 * 
	 * @param dd0
	 * @param dd1
	 * @param dd2
	 * @param dd3
	 * @author Juan Carlos
	 * @link juan.carlos.conde.colque@gmail.com
	 */
	configureDropDown: function(dd0, dd1, dd2, dd3) {
		$.ajax({
			url: $(dd1).attr('srcmodules'),
			type: "POST",
			beforeSend : function(XMLHttpRequest){
				
			},
			success : function(html, textStatus, XMLHttpRequest) {
				if (textStatus == 'success') {
					$(dd1).html(html);
				}
			},
			complete : function(jqXHR, textStatus) {
				
			},
			error : function(jqXHR, textStatus, errorThrown) {
				// TODO: cambiar texto a ingles
				alert.show(errorThrown,{header : com.em.Alert.ERROR});
			}
		});
		
		var table = this.table;
		var url = this.url.toTable;
		// configure drop down roles
		$(dd0).change(function() {
			
			var settings = table.fnSettings();
			if ($(dd0).val() == "") {
				settings.sAjaxSource = url + '/role/0';
			} else {
				settings.sAjaxSource = url + '/role/' + $(this).val();
			}
			table.fnDraw();
		});
		
			// configure drop down modules and controllers
		$(dd1 + ',' + dd2).change(function(e) {
			var module = '';
			var controller = '';
			var url = $(this).attr('src');
			if ($(this).attr('id') == 'moduleFilter') {
				url = url + '/module_name/' + $(this).val(); 
			} else if ($(this).attr('id') == 'controllerFilter') {
				url = url + '/module_name/' + $(dd1).val() + '/controller_name/' + $(dd2).val();
			}
			var dd = $(this);
			$.ajax({
				url: url,
				type: "POST",
				beforeSend : function(XMLHttpRequest){
					
				},
				success : function(html, textStatus, XMLHttpRequest) {
					if (textStatus == 'success') {
						if (dd.attr('id') == 'moduleFilter') {
							$(dd2).html(html); 
						} else if (dd.attr('id') == 'controllerFilter') {
							$(dd3).html(html); 
						}
					}
				},
				complete : function(jqXHR, textStatus) {
					
				},
				error : function(jqXHR, textStatus, errorThrown) {
					// TODO: cambiar texto a ingles
					alert.show(errorThrown,{header : com.em.Alert.ERROR});
				}
			});
		});
	},
	
	/**
	 * Show proccessing display for data table 
	 * 
	 * @param bShow boolean
	 */
	processingDisplay : function(bShow) {
		var settings = this.table.fnSettings();
		settings.oApi._fnProcessingDisplay(settings,bShow);
	},
	
	/**
	 * Delete n items
	 * @param selector
	 */
	clickToAdd : function(selector) {with (this) {
		$(selector).bind('click',function(event) {
			event.preventDefault();
			var action = $(this).attr('href');
			$.ajax({
				url: action,
				type: "POST",
				dataType : 'json',
				data : $('#form-privilege').serialize(),
				beforeSend : function(XMLHttpRequest){
					validator.form();
					if ( !validator.valid() ) {
						return false;
					}
					processingDisplay(true);
					return true;
				},
				success : function(data, textStatus, XMLHttpRequest) {
					if (textStatus == 'success') {
						if ( data.success ) {
							table.fnDraw();
							alert.show(data.message);
						} else {
							alert.show(data.message, {header : com.em.Alert.FAILURE});
						}
					}
				},
				complete : function(jqXHR, textStatus) {
					processingDisplay(false);
				},
				error : function(jqXHR, textStatus, errorThrown) {
					alert.show(errorThrown,{header : com.em.Alert.ERROR});
				}
			});
		});
	}},
	
	/**
	 * Delete n items
	 * @param selector
	 */
	clickToDelete : function(selector) {with (this) {
		$(selector).bind('click',function(event) {
			event.preventDefault();
			var items = $('#tblprivileges :checked').parent().parent();
			$.each(items,function(i, item) {
				table.fnDeleteRow(item);
			});
		});
	}},
	
	/**
	 * Check if table is empty
	 * @returns {Boolean}
	 */
	isEmpty : function() {
		return this.table.fnSettings().aoData.length > 0;
	},
	
	/**
	 * set url for request new action
	 * @param url json
	 */
	setUrl : function(url) {
		this.url = url;
	},
};